package stream;

import jvm.classLoader.Student;
import org.w3c.dom.ls.LSOutput;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamTest{

    public static void main(String[] args) {
        //中间操作：filter+终止操作：collect
        List<Integer> integers = Arrays.asList(4, 2, 3,2,5,6, 4, 5);
        List<Integer> collect = integers.stream().filter(i -> i > 3).collect(Collectors.toList());
        System.out.println(collect);
        //distinct
        List<Integer> collect1 = integers.stream().filter(i -> i > 3).distinct().collect(Collectors.toList());
        System.out.println(collect1);
        //limit
        List<Integer> collect2 = integers.stream().filter(i -> i > 3).distinct().limit(2).collect(Collectors.toList());
        System.out.println(collect2);
        //skip
        List<Integer> collect3 = integers.stream().skip(2).filter(i -> i > 3).collect(Collectors.toList());
        System.out.println(collect3);
        //map
        List<String> list = Arrays.asList("j,i,m", "J,a,n,e");
        List<String> collect4 = list.stream().map(t -> t.replaceAll(",", "")).collect(Collectors.toList());
        System.out.println(collect4);
        //sorted
        List<String> list1 = Arrays.asList("aa", "ff", "dd");
        list1.stream().sorted().forEach(System.out::println);

        Student s1 = new Student("aa", 10);
        Student s2 = new Student("bb", 20);
        Student s3 = new Student("aa", 30);
        Student s4 = new Student("bb", 10);
        List<Student> studentList = Arrays.asList(s1, s2, s3, s4);
        studentList.stream().sorted(
                (o1, o2) -> {
                    if (o1.getName().equals(o2.getName())) {
                        return o1.getAge() - o2.getAge();
                    } else {
                        return o1.getName().compareTo(o2.getName());
                    }
                }
        ).forEach(c-> {
            System.out.println(c.getName()+c.getAge());
        });


        //allMatch
        boolean b = integers.stream().allMatch(i -> i > 3);
        System.out.println(b);

        boolean b1 = integers.stream().anyMatch(i -> i > 3);
        System.out.println(b1);

        boolean b2 = integers.stream().noneMatch(i -> i > 3);
        System.out.println(b2);

        //终止操作
        List<Integer> integers1 = Arrays.asList(1, 2, 3);
        Integer integer = integers.stream().reduce((x, y) -> x + y).get();
        System.out.println(integer);

        //collect
        String collect5 = studentList.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect5);

        //分组
        Map<Integer, List<Student>> collect6 = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
        System.out.println(collect6);

        //分区
        Map<Boolean, List<Student>> collect7 = studentList.stream().collect(Collectors.partitioningBy(t -> t.getAge() > 10));
        System.out.println(collect7);
    }

}
